package com.ocom.company.controller;


import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.dto.CompanyInfoDto;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.wx.RegisterPageRegisterRequest;
import com.ocom.common.request.wx.UnBindRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.company.service.CompanyInfoService;
import com.ocom.company.service.CompanyListInfoService;
import com.ocom.company.service.CompanyPartService;
import com.ocom.company.service.WechatPerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WxCompanyController {

    @Autowired
    CompanyListInfoService companyListInfoService;

    @Autowired
    CompanyPartService companyPartService;

    @Autowired
    private WechatPerInfoService wechatPerInfoService;

    @Autowired
    CompanyInfoService companyInfoService;





    @PostMapping(CompanyServiceUrl.URL_Wx_AddCompanyInfo)
    public Result addCompany(@Valid  @RequestBody RegisterPageRegisterRequest request){

        boolean b=companyListInfoService.isExistName(request.getCompanyName());
        if(b){
            return ResultUtil.error(ResultEnum.Err_308);
        }

        //选库
        Long comId = companyPartService.GetComID();
        CompanyInfoDto companyInfoDto=new CompanyInfoDto();
        companyInfoDto.setCreateUserid(1L);
        companyInfoDto.setComName(request.getCompanyName());

        //1.分库插入
        companyInfoService.AddCompanyInfo(companyInfoDto,comId);

        //2.总库插入
        companyListInfoService.AddCompanyListInfo(companyInfoDto);

        //3.添加管理员   mng_user_info
        EntityMngUserInfo entityMngUserInfo=new EntityMngUserInfo();
        entityMngUserInfo.setComId(companyInfoDto.getComId());


        Map<String,Object> map=new HashMap<>();
        map.put("companyId",companyInfoDto.getComId());
        map.put("pId",1);
        map.put("pRoles",21);

        return ResultUtil.success(map);
    }


    /**
     * 修改颜色
     * @param companyId
     * @return
     */
    @PostMapping(CompanyServiceUrl.URL_modifyColor)
    public Result  modifyColor(Long companyId,String colorNo){
        //总库更新
        companyListInfoService.modifyColor(companyId,colorNo);
        //分库更新
        companyInfoService.modifyColor(companyId,colorNo);
        return ResultUtil.success();
    }

    /**
     * 修改名称
     * @param companyId
     * @return
     */
    @PostMapping(CompanyServiceUrl.URL_modifyName)
    public Result  modifyName(Long companyId,String companyName){
        boolean isExist=companyListInfoService.isExistName(companyName);
        if(isExist){
            return ResultUtil.error(ResultEnum.Err_2007);
        }
        //总库更新
        companyListInfoService.modifyName(companyId,companyName);
        //分库更新
        companyInfoService.modifyName(companyId,companyName);
        return ResultUtil.success();
    }


    /**
     * 删除管理员绑定
     * @param companyId
     * @return
     */
    @PostMapping(CompanyServiceUrl.URL_unbingWxCompanyAdmin)
    public Result  unbingWxCompanyAdmin(@RequestBody UnBindRequest request){
        wechatPerInfoService.delWxUser(request.getCompanyId(),request);
        return ResultUtil.success();
    }




}
